package com.ruoyi.system.order;



import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCBNJN6j1f5YKh38Brzj/+iFIxuu9aaYe7oBR3ev2VZld6dFRwJADV4+uLlWAU0L31I4pIp4PBof+yVNYBna6uebzUwPPdgDR91lPa6dO/O3ARtPcucwRNold7ieAEi7Y80xemgJmmyfgXqYU8bVedGdNVuNlHmydG1zFLn4PMN8Qz4fg0YJ6uOhr/Uq50/45x4Qcmtd7/eAE6twDhaj/VGIZgjFeG7x3ICclqcrFGWFZr7oXVrrF/35fD5MzmzaO6x4OMGtTS7Dhf3OAXN7IGVboL7j2IzBahBaW9HDjsEqZJLCRXB+EyNHAt5Q0BJ9hVsb7gNZZ/xtzZUSvde+6p5AgMBAAECggEAE/QTpoTGmEuWGNeqwdyGJPi5a0x06uie5ZQMtd6m/zvG4kllR/qRR3qvWa3LN/DxjzT2T6NeXTUMS0q05sPZACG6fBWeOmYsapMzuz3d3vtZb6lwbWff4HHsttrDiipXyFWcK9Bi2F7f+P9IITeV/OqEH34iDdszGLlSQF8cChXsqiql40z49ERmWMJowrXHXcQyW/id2bBZ4JskZtxBd7qwnobWOHetH7T+V6WS3gZnapoy08cMGXQP1fFEzb6vvAP76w9vVHBKiUt5u+SFX6PqpImvFORFHS+9YMqJC+j+5/pPjNwE1CTsHKnl9+DimLUTtA2Cf7RtvBq191PdlQKBgQC91ePbDJFIQK9o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LqzAkGvFLTp2NT3W+DPiUmIsnxAGSqieP31xOdTuwxh0yBuB3Wdsuusce+wFJ9hEud09wHUEO02P6MHyZkTA0PPIm0FhiO1BJL6vObLxINRUKkJ0tt8Bb1M2RppjP1kMFdjbTRtODDqHXpVAamXLef9KmmbGdvTNrbWZZCBHQ5a3dil4qQNOT5nwIDAQAB";

    public static final String OUT_TRADE_NO = "200100234567910123";

    //生成支付订单接口
    public Map pay() throws AlipayApiException {
    //public Map pay(PayData payData) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000140639237");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        /*model.setOutTradeNo(payData.getCode());
        model.setTotalAmount(payData.getMoney().toString());
        model.setSubject(payData.getName());*/

        model.setOutTradeNo(OUT_TRADE_NO);
        model.setTotalAmount("0.01");
        model.setSubject("测试产品");

        model.setProductCode("2205A");
        request.setBizModel(model);

        //后台配置文件不行
        //request.setNotifyUrl("https://71a6-61-181-211-208.ngrok-free.app/alipay");

        //回调地址
        request.setNotifyUrl("http://2d3rts.natappfree.cc/alipay/notify");




        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);

        System.out.println("==============================");
        System.out.println(response.getBody());
        System.out.println("===============================");

        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code",200);
            map.put("form",response.getBody());
        } else {
            map.put("code",400);
        }
        return map;
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000140639237");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);

        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }


     public static void main(String[] args) {
        try {
            ZhifubaoUtill utill = new ZhifubaoUtill();
            utill.pay();
            //utill.queryResult(OUT_TRADE_NO);
            Thread.sleep(1000);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
